
package managedBean;

import Merlion.CRM.entity.LineEntryEntity;
import Merlion.util.state.InvoiceAndReceiptState;
import Merlion.util.state.SalesDocumentState;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;

@ManagedBean(name = "InvoiceDisplayManagedBean")
@ViewScoped
public class InvoiceDisplayManagedBean implements Serializable {

    private Long invoiceId;
    private Long salesOrderId;
    private Double cashInAdvance;
    private Double discountRate;
    private Double totalPrice;
    private Date issueDate;
    private Date invoiceDueDate;
    private int creditTerm = 30;
    private int discountedCreditTerm = 0;
    private Double amountDue;
    private boolean invoiceStatus = false;
    private String deleteFlag;
    
    private String customerName;
    
    private InvoiceAndReceiptState selectedInvoice;
    private List<InvoiceAndReceiptState> selectedInvoices;
    private InvoiceAndReceiptState[] selectedInvoicesArray;
    private InvoiceAndReceiptState selectedViewInvoice;
    private boolean editMode;
    
    private Collection<LineEntryEntity> selectedProductList;
    
    private List<SalesDocumentState> salesOrders;

    public InvoiceDisplayManagedBean() {
    }

    public void doDisplay(PhaseEvent event) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            if (invoiceId == null) {
                invoiceId = (Long) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("invoiceId");
            }
            if (salesOrderId == null) {
                salesOrderId = (Long) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("salesOrderId");
            }
            if (cashInAdvance == null) {
                cashInAdvance = (Double) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("cashInAdvance");
            }
            if (discountRate == null) {
                cashInAdvance = (Double) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("discountRate");
            }
            if (totalPrice == null) {
                cashInAdvance = (Double) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("totalPrice");
            }
            if (issueDate == null) {
                issueDate = (Date) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("issueDate");
                
            }
            if (invoiceDueDate == null) {
                invoiceDueDate = (Date) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("invoiceDueDate");
            }
            if (creditTerm == 30) {
                creditTerm = (Integer) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("creditTerm");
                //creditTerm = Integer.parseInt(ct);
            }
            if (discountedCreditTerm == 0) {
                discountedCreditTerm = (Integer) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("discountedCreditTerm");   
            }
            if (amountDue == null) {
                amountDue = (Double) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("amountDue");
            }
            if (invoiceStatus == false) {
                String is = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("invoiceStatus");
                invoiceStatus = Boolean.parseBoolean(is);
            }
            if (deleteFlag == null) {
                deleteFlag = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("deleteFlag");
            }
            if (selectedInvoice == null) {
                selectedInvoice = (InvoiceAndReceiptState) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedInvoice");
            }
            if (selectedInvoices == null) {
                selectedInvoices = (List<InvoiceAndReceiptState>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedInvoices");
            }
            if (selectedInvoicesArray == null) {
                selectedInvoicesArray = (InvoiceAndReceiptState[]) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedInvoicesArray");
            }
            if (selectedViewInvoice == null) {
                selectedViewInvoice = (InvoiceAndReceiptState) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedViewInvoice");
            }
            if (selectedProductList == null) {
                selectedProductList = (Collection<LineEntryEntity>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedProductList");
            }
            if (salesOrders == null) {
                salesOrders = (List<SalesDocumentState>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("salesOrders");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Double getAmountDue() {
        return amountDue;
    }

    public void setAmountDue(Double amountDue) {
        this.amountDue = amountDue;
    }

    public Double getCashInAdvance() {
        return cashInAdvance;
    }

    public void setCashInAdvance(Double cashInAdvance) {
        this.cashInAdvance = cashInAdvance;
    }

    public int getCreditTerm() {
        return creditTerm;
    }

    public void setCreditTerm(int creditTerm) {
        this.creditTerm = creditTerm;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getDeleteFlag() {
        return deleteFlag;
    }

    public void setDeleteFlag(String deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public Double getDiscountRate() {
        return discountRate;
    }

    public void setDiscountRate(Double discountRate) {
        this.discountRate = discountRate;
    }

    public int getDiscountedCreditTerm() {
        return discountedCreditTerm;
    }

    public void setDiscountedCreditTerm(int discountedCreditTerm) {
        this.discountedCreditTerm = discountedCreditTerm;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public Date getInvoiceDueDate() {
        return invoiceDueDate;
    }

    public void setInvoiceDueDate(Date invoiceDueDate) {
        this.invoiceDueDate = invoiceDueDate;
    }

    public Long getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(Long invoiceId) {
        this.invoiceId = invoiceId;
    }

    public boolean isInvoiceStatus() {
        return invoiceStatus;
    }

    public void setInvoiceStatus(boolean invoiceStatus) {
        this.invoiceStatus = invoiceStatus;
    }

    public Date getIssueDate() {
        return issueDate;
    }

    public void setIssueDate(Date issueDate) {
        this.issueDate = issueDate;
    }

    public Long getSalesOrderId() {
        return salesOrderId;
    }

    public void setSalesOrderId(Long salesOrderId) {
        this.salesOrderId = salesOrderId;
    }

    public InvoiceAndReceiptState getSelectedInvoice() {
        return selectedInvoice;
    }

    public void setSelectedInvoice(InvoiceAndReceiptState selectedInvoice) {
        this.selectedInvoice = selectedInvoice;
    }

    public InvoiceAndReceiptState[] getSelectedInvoicesArray() {
        return selectedInvoicesArray;
    }

    public void setSelectedInvoicesArray(InvoiceAndReceiptState[] selectedInvoicesArray) {
        this.selectedInvoicesArray = selectedInvoicesArray;
    }

    public InvoiceAndReceiptState getSelectedViewInvoice() {
        return selectedViewInvoice;
    }

    public void setSelectedViewInvoice(InvoiceAndReceiptState selectedViewInvoice) {
        this.selectedViewInvoice = selectedViewInvoice;
    }

    public Double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public List<SalesDocumentState> getSalesOrders() {
        return salesOrders;
    }

    public void setSalesOrders(List<SalesDocumentState> salesOrders) {
        this.salesOrders = salesOrders;
    }

    public List<InvoiceAndReceiptState> getSelectedInvoices() {
        return selectedInvoices;
    }

    public void setSelectedInvoices(List<InvoiceAndReceiptState> selectedInvoices) {
        this.selectedInvoices = selectedInvoices;
    }

    public Collection<LineEntryEntity> getSelectedProductList() {
        return selectedProductList;
    }

    public void setSelectedProductList(Collection<LineEntryEntity> selectedProductList) {
        this.selectedProductList = selectedProductList;
    }
}
